package com.telenav;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import javax.imageio.ImageIO;

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.events.KeyAdapter;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;

public class HumanRgnImageUI extends Shell {
	private Text text;
	private File[] files;
	private int current = 0;
	private int max = 0;
	private Canvas canvas_all;
	protected boolean start = false;

	/**
	 * Launch the application.
	 * @param args
	 */
	public static void main(String args[]) {
		try {
			Display display = Display.getDefault();
			HumanRgnImageUI shell = new HumanRgnImageUI(display);
			shell.open();
			shell.layout();
			while (!shell.isDisposed()) {
				if (!display.readAndDispatch()) {
					display.sleep();
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Create the shell.
	 * @param display
	 * @throws Exception 
	 */
	public HumanRgnImageUI(Display display) throws Exception {
		super(display, SWT.SHELL_TRIM);
		setLayout(null);

		canvas_all = new Canvas(this, SWT.NONE);
		canvas_all.setBounds(270, 64, 60, 40);

		text = new Text(this, SWT.BORDER);
		text.addKeyListener(new KeyAdapter() {
			@Override
			public void keyReleased(KeyEvent e) {
				if (e.keyCode == 13 ) {
					try {
						File f = files[current];
						String str = files[current-1].getName() + "," + text.getText();
						System.out.println(str);
						files[current-1].renameTo(new File("E:/failed/recongized/"+text.getText()+"_"+System.currentTimeMillis()+".bmp"));
						if (current < max - 1) {
							loadNext(f);
						} else {
							MessageBox mb = new MessageBox(getShell());
							mb.setMessage("finished");
							mb.open();
							return;
						}
						text.setText("");
					} catch (Exception e1) {
						e1.printStackTrace();
					}
				}
			}
		});
		text.setBounds(270, 127, 73, 21);
		
		Button btnStart = new Button(this, SWT.NONE);
		btnStart.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				File f = files[current];
				loadNext(f);
			}
		});
		btnStart.setBounds(255, 188, 75, 25);
		btnStart.setText("start");
		createContents();
		init();
	}

	private void init() {
		File file = new File("E:/failed/failed");
		files = file.listFiles();
		current = 0;
		max = files.length;
	}

	protected void loadNext(File f) {
		try {
			loadImage(f);
		} catch (Exception e) {
			e.printStackTrace();
		}
		current++;

	}

	private void loadImage(File f) throws Exception {
		BufferedImage image = ImageIO.read(f);
		Image swtimage = new Image(Display.getDefault(), ImgProcessUI.convertToSWT(image));
		GC gc = new GC(canvas_all);
		gc.setForeground(new Color(Display.getDefault(), new RGB(255, 255, 255)));
		gc.drawImage(swtimage, 0, 0);
	}

	/**
	 * Create contents of the shell.
	 */
	protected void createContents() {
		setText("SWT Application");
		setSize(647, 453);


	}

	@Override
	protected void checkSubclass() {
		// Disable the check that prevents subclassing of SWT components
	}
}
